<?php


namespace backend\controllers;


use backend\models\DisStars;
use \yii\web\Response;
use yii\filters\VerbFilter;
use yii\web\Controller;

class AjaxController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    public function actionRating($id,$star)
    {
        $admin_id=\Yii::$app->user->getId();
        $model=DisStars::find()->where(['admin_id'=>$admin_id,'works_id'=>$id])->one();
        if(!$model)
        {
            $model=new DisStars();
        }
        $model->admin_id=$admin_id;
        $model->works_id=$id;
        $model->score=$star;
        if($model->validate()&&$model->save())
        {
            \Yii::$app->response->format=Response::FORMAT_JSON;
            return ['msg'=>true];
        }else
        {
            \Yii::$app->response->format=Response::FORMAT_JSON;
            return ['msg'=>'数据不合法！'];
        }
    }
}